package com.alumni_circle.controller.home;

import cn.hutool.core.util.StrUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.Modify;
import com.alumni_circle.core.utils.PictureBase64;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.News;
import com.alumni_circle.entity.home.VO.NewsInsertVO;
import com.alumni_circle.entity.home.VO.NewsViewVO;
import com.alumni_circle.entity.home.issue.UserNews;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.service.home.INewsService;
import com.alumni_circle.service.home.issue.IUserNewsService;
import com.alumni_circle.utils.MsgPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

import static com.alumni_circle.core.constant.ResponseCode.PERMISSIONS;

/**
 * @author 龙小虬
 * @since 2020-07-06 22:58
 */
@Api(tags = "新闻管理")
@Controller
@RequestMapping("/news")
public class NewsController extends BaseController<News> {

    @Resource
    private INewsService newsService;

    @Resource
    private IUserNewsService userNewsService;

    @Resource
    MsgPermissions msgPermissions;

    /**
     * 增加信息
     * @param newsInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增新闻信息", notes = "增加新闻信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="新增新闻信息",url="/news/addNews")
    public ResponseModel addNews(@ApiParam(required = true, name = "newsInsertVO")
                                      @RequestBody NewsInsertVO newsInsertVO) {
        News news = new News();
        news.setTitle(newsInsertVO.getTitle());
        news.setContent(newsInsertVO.getContent());
        String path = "";
        if(newsInsertVO.getPictures()!=null && newsInsertVO.getPictures().contains("{-temporary}")) {
            path = newsInsertVO.getPictures();
            news.setPicture(Modify.modifyImageIndex(path));
        }
        User user = SecurityUtils.getUser();
        news.setCreateBy(user.getId());
        news.setCreateName(user.getName());
        newsService.save(news);
        super.picturePath(0,news.getId(),path);
        //拿取news的id
        System.out.println(news.getId());
        UserNews userNews = new UserNews();
        userNews.setNewsId(news.getId());
        userNews.setUserId(user.getId());
        userNewsService.save(userNews);
        return new ResponseModel<>(news);
    }

    /**
     * 更新信息
     * @param newsInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新新闻信息", notes = "更新新闻信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新新闻信息",url="/news/updateNews")
    public ResponseModel updateNews(@ApiParam(required = true, name = "newsInsertVO")
                                         @RequestBody NewsInsertVO newsInsertVO) {

        if(msgPermissions.updateDelete(newsInsertVO.getId(), "news")){
            return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
        }
        newsService.updateById(ModelUtils.transform(newsInsertVO,News.class));
        return new ResponseModel<>("success");
    }

    /**
     * id查信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询新闻信息", notes = "查询新闻信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询新闻信息",url="/news/selectNews")
    public ResponseModel<Object> selectNews(@ApiParam(required = true, name = "baseId")
                                         @RequestBody BaseId baseId) {

        News news = newsService.getById(baseId.getId());
        return new ResponseModel<>(ModelUtils.transform(news, NewsInsertVO.class));
    }


    /**
     * 删除信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除新闻信息", notes = "删除新闻信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除新闻信息",url="/news/deleteNews")
    public ResponseModel deleteNews(@ApiParam(required = true, name = "baseId")
                                         @RequestBody BaseId baseId) {

        if (msgPermissions.updateDelete(baseId.getId(), "news")){
            return new ResponseModel<>(PERMISSIONS.getCode(),PERMISSIONS.getMessage());
        }
        newsService.removeById(baseId.getId());
        QueryWrapper<UserNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("news_id",baseId.getId());
        userNewsService.remove(queryWrapper);
        super.picturePathDelete(baseId.getId());
        return new ResponseModel();
    }


    /**
     * 查询所有新闻信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有新闻信息", notes = "查询所有新闻信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询所有新闻信息",url="/news/getAllNews")
    public ResponseListModel getAllNews(@ApiParam(required = true, name = "pageBase")
                                             @RequestBody PageBase pageBase) {

        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<NewsViewVO> list = newsService.getAllNews();
        PageInfo<NewsViewVO> pageInfo = new PageInfo<>(list);
        for (NewsViewVO news : list) {
            if (!StrUtil.hasEmpty(news.getPicture())) {
                news.setPicture(PictureBase64.thumbnailList(news.getPicture()));
            }

        }
        return new ResponseListModel<>(ModelUtils.copyListProperties(list, NewsViewVO.class),String.valueOf(pageInfo.getTotal()));
    }


    /**
     * 搜索自己发布的新闻
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectMyListNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询符合条件新闻信息", notes = "查询符合条件新闻信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询符合条件新闻信息",url="/news/selectMyListNews")
    public ResponseListModel selectMyListNews(@ApiParam(required = true, name = "pageBase")
                                        @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<NewsViewVO> list = newsService.selectMyListNews(SecurityUtils.getId());
        for (NewsViewVO newsViewVO : list) {
            if (!StrUtil.hasEmpty(newsViewVO.getPicture())) {
                newsViewVO.setPicture(PictureBase64.thumbnailList(newsViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }

    /**
     * 搜索自己发布的新闻
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchMyListNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询符合条件新闻信息", notes = "查询符合条件新闻信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询符合条件新闻信息",url="/news/searchMyListNews")
    public ResponseListModel searchMyListNews(@ApiParam(required = true, name = "pageBase")
                                              @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<NewsViewVO> list = newsService.searchMyListNews(SecurityUtils.getId(),pageBase.getContent());
        for (NewsViewVO newsViewVO : list) {
            if (!StrUtil.hasEmpty(newsViewVO.getPicture())) {
                newsViewVO.setPicture(PictureBase64.thumbnailList(newsViewVO.getPicture()));
            }

        }
        return new ResponseListModel<>(list);
    }



    @ResponseBody
    @RequestMapping(value = "/searchNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索新闻信息", notes = "搜索新闻信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索新闻信息",url="/news/searchNews")
    public ResponseListModel searchNews(@ApiParam(required = true, name = "pageBase")
                                        @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        return new ResponseListModel<>(ModelUtils.copyListProperties(
                newsService.list(new QueryWrapper<News>().and(wrapper -> wrapper.like("title",pageBase.getContent()).or().like("content",pageBase.getContent()))),NewsViewVO.class));
    }


}
